package com.example.wangxin.mytimeline;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 创建日期: 2018/2/2 on 10:01.
 * 作者:wangxin
 */

public class DashLineView extends View {
    public Context mContext;
    public int mStartWidth;
    public int mTop;
    public int mEnd;
    private Paint mPaint;
    private Path mPath;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(mPath,mPaint);
    }

    public DashLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public DashLineView(Context context) {
        super(context,null);
    }
    public void initView() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(R.color.lightgray));
        mPaint.setStrokeWidth(dip2px(mContext, 2));
        mPath = new Path();
//        mPath.moveTo(mStartWidth, mTop);
//        mPath.lineTo(mStartWidth, mEnd);
        PathEffect effects = new DashPathEffect(new float[]{6, 4, 4, 4}, 2);
        mPaint.setPathEffect(effects);
    }

    public void drawDotLine(int startWidth,int top,int end) {
        mStartWidth = startWidth;
        mTop = top;
        mEnd = end;
        mPath.moveTo(mStartWidth, mTop);
        mPath.lineTo(mStartWidth, mEnd);
        Log.e("aaaa", "width + " + mStartWidth + ",mTop + " + mTop + ",mEnd + " + mEnd);
        invalidate();
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
